Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

PHP & MySQL newb needs help

Name: Anonymous 2005-12-13 5:14

Anyone know a good script that would take data from a mysql table, import it into a php array, and then sort it?  I found tutorials on how to print mysql data, and understood that easily enough... and then found a tutorial on how to sort an array in php, and that made enough sense, but I can't seem to figure out how to connect the two.

Here's the relevant bits of code:

<?
$username="username";
$password="password";
$database="your_database";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

$i++;
}

?>

<?php
// demo array to sort
$latest_array = array(
array('news','1234567890','sdf','asdpojq'),
array('news','1234567892','uruy','xbuqiwpo'),
array('comment','1234567893','fghj','mjktyu'),
array('article','1234567891','cvmo','pjklgg'),
array('news','1234567894','qwenb','asbhtyhj'),
);
$sort_field = 3; // enter the number of field to sort
// compare function
function cmpi($a, $b)
{
    global $sort_field;
    return strcmp($a[$sort_field], $b[$sort_field]);
}
// do the array sorting
usort($latest_array, 'cmpi');
// demo output
echo '<pre>';
print_r($latest_array);
?>

Name: Anonymous 2005-12-13 5:33

no. nobody knows. go away.

Name: Anonymous 2005-12-13 6:19

>>1
You mean something like mysql_fetch_array() and sort()?
Also you fail for using mysql and for having shitty php code.
Now go away and die.

Name: Anonymous 2005-12-13 7:12

Stop using php
Stop using mysql
You're probably using Apache; stop using that too.

Ruby on Rails + Postgres + lighttpd

Name: Anonymous 2005-12-13 12:24

>>4
pfft.
Clearly Java + Spring + JSF + Hibernate + Oracle + SUN AS + EJB3


Oh yeah, that'll be $1 million in consulting fees too.

Name: Anonymous 2005-12-13 12:47

>>5
I'd use that stack if I were being paid by the hour, or per line of code. PROFIT!

Name: Anonymous 2005-12-13 23:39

>>1
I can't even imagine someone who can't figure this out, so I'm not very good at answering this question, but replace
echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

with the obvious part of the second piece of code.

Then ignore people telling you to use Ruby, because it's too Web 2.0 (but mod_perl is pretty cool), and MySQL is just fine for doing stupid trivial stuff like this. lighttpd is pretty cool though.

Name: Anonymous 2005-12-14 3:09

>>7
I think "Web 2.0" works best if you ignore all the people who are using the term, as well as the term itself. Paul Graham has a nice essay regarding this (which is where I found out about the term; I hadn't even heard it previously) where he says that we haven't upgraded to a new version of anything; we're just doing it how it's supposed to be done. And why should we require special terminology to describe things not being botched?

Name: Anonymous 2005-12-14 4:31 (sage)

And what's wrong with 'ORDER BY' ?

Name: Anonymous 2005-12-14 8:12

Huh? Ruby and Web 2.0?

Besides "Web 2.0" being a totally fagtastic term (actually, that's being totally unfair to faggots), what does Ruby have to do with it? It's just another language.

Name: Anonymous 2005-12-14 10:11

>>10
I assume >>7 was talking more about Rails than Ruby. Rails could easily be marketed as "the premier platform for Web 2.0 applications" or some similar bullshit. It's got "Ajax helpers" and everything.

Name: Anonymous 2005-12-14 10:37

I feel sorry for Rails. Every last blogging retard is looking for something to validate their worthless drivel. If they can stuff themselves under the same umbrella as Rails, they'll do it.

I feel sorry for Javascript too. zOMG Ajax!

Name: Anonymous 2005-12-14 12:00

I'd like to see the j in Ajax become as flexible as the P in LAMP. One language isn't enough.

Name: Anonymous 2005-12-14 13:20

>>6
Lol on truth, the master plan has been completed:
1. Get paid per line using J2EE
2. Write a million lines of code (for a small application)
3. PROFIT

>>8
>>9
>>10
>>13
This thread is full of truth

Name: Anonymous 2005-12-15 11:56

>>6
>>14
You guys aren't doing anything until I get my million bucks!

Name: Anonymous 2005-12-22 22:34 (sage)

>>ALL
STFU & GTFO NOOBS
you know nothing.

Name: Anonymous 2006-03-09 18:35

>>1
nab the 'hack' from codegrrl.com phpfanbase for expoting the memberlist & customise it to your needs. It simply prints a page of comma seperated values sorted by whatever you want.

Name: Anonymous 2006-03-09 20:50

>>17
Insecure.org's logo is cooler.

Name: Anonymous 2009-09-19 17:39

I was going to do a very sloppy (probably wouldn't work) OOP version for OP, but I got lazy. A lot of the script is kinda.. theoretical? I am not used to PHP's oop, but I thought I would give it a go. You guys an mess with it. Can't guarantee it will work any.

class Connect {
   
    public $mysqli;
    private $host = "localhost";
    private $user = "user";
    private $pass = "pass";
    private $name = "name";
   
    function Build ($i="")  {
        switch ($i) {
            case "connect":
                $this->mysqli = new mysqli($host, $user, $pass, $name);
                # Add your own errors
                break;
            case "disconnect":
                $this->mysqli->close();
                break;
            }
        }
    }

class Main extends Connect {
   
    private $query, $result, $num;
   
    function __construct () {
        Connect::Build("connect");
    }
   
    function Query () {
        $this->query  = "select * from contacts";
        $this->result = Connect::Build("connect")->query($this->query);
        $this->num    = Connect::Build("connect")->num_rows($this->result);
    }
   
    function __destruct () {
        Connect::Build("disconnect");
    }
}

Name: Anonymous 2009-09-19 18:25

>>18
I enjoy the nand.net's logo.

Name: Anonymous 2009-09-19 22:08

Here's the first part, you can 'sort it out' from here, no pun intended:

<?php

$username="username";
$password="password";
$database="your_database";

mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);
$num=@mysql_num_rows($result); //you missed an underscore
$rows=array();

//mysql_close(); //why close already?

echo "<b><center>Database Output: rows: $num</center></b><br><br>";

//$i=0;

//while ($i < $num) {
//since your variables are spelled the same..
while ($data=mysql_fetch_assoc($result)) {
foreach($data as $k=>$v) { $$k=$v; }

$rows[]=$data;

/*
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");//*/

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

//$i++;
}

echo "<pre>".print_r($rows,1)."</pre>";

Name: Anonymous 2009-09-19 22:11

>>4
lighttpd would be fine if it didn't leak memory like a sieve.
Use nginx.

Name: Anonymous 2009-09-19 22:16

1  Name: Anonymous : 2005-12-13 05:14 
19  Name: Anonymous : 2009-09-19 17:39 
21  Name: Anonymous : 2009-09-19 22:08 

Name: Anonymous 2010-12-21 5:01

Name: Anonymous 2011-02-04 18:22

<

Name: Anonymous 2011-04-02 17:43

$bump;

Name: Anonymous 2011-04-02 19:56

>>22
For some reason I think it's due to GNU libc's shitty malloc. But maybe that's just me.

Don't change these.
Name: Email:
Entire Thread Thread List