...

View Full Version : PHP/MYSQL quick help



Jozh
04-19-2003, 12:57 AM
Ok I got a databased named 'test_DB' and a table inside of that named 'Table1'. Inside of 'Table1' I have 2 feilds 'ID,Entry'.

Now on my php page How would I display a selected entry from its ID number which is from the ID column, and display it?


Thanks, Josh

Jason
04-19-2003, 01:24 AM
here, I barrowed this from the MYSQL forum...look harder next time for your answer...

$link = @mysql_pconnect($server, $user, $pwd)

or die("Could not connect: " . mysql_error());

@mysql_select_db($db,$link)

or die ("Could not select database." . mysql_error());

$sql_select = "select * from table ";

$result = mysql_query($sql_select, $link);

if ($result){

echo mysql_num_rows($result) ;

}

else {

echo ("Problem: " . mysql_error());

}


Jason

Jozh
04-19-2003, 01:50 AM
That little script can display both?? :-O?

Jason
04-19-2003, 01:55 AM
In your studying of MySQL you should have atleast learned about the SELECT statements, they are universal throught all dbs as far as I know. Now the SELECT statment you have here is SELECT * where the * reperesents all the information in the table...so if you look for a certain ID you would need something like a WHERE clause to say SELECT * WHERE ID==num or something. Not to positive on teh syntax but that will get the entire row where that ID has the same value as num...does that make sense?


Jason

Jozh
04-19-2003, 01:55 AM
hmm

@mysql_select_db($db,$link)

Ive never seen the $link what the hell does that mean?

Jason
04-19-2003, 01:59 AM
the $link is basically getting its information elsewhere, in PHP the $link will be a variable with the title link...in our case here its refering to $link = @mysql_pconnect($server, $user, $pwd) so it is making sure that you are connected to the MySQL server... make sense?

Jason

Jozh
04-19-2003, 02:02 AM
Bah I dont understand much of this... :( Can you just plug the info in for me?

Would help lots thanks.

Jozh
04-19-2003, 02:37 AM
:-(

Spookster
04-19-2003, 08:28 PM
Originally posted by Jozh
Bah I dont understand much of this... :( Can you just plug the info in for me?

Would help lots thanks.

It would seem that you don't wish to learn this in which case indicates to me that this could be a school homework assigment where you just want us to give you the answer. If that is the case we are done helping you.

It's not that difficult to understand. You should read through some basic SQL and PHP tutorials. There are plenty of threads that deal with both in these forums so you can use the search feature.

ASAAKI
04-19-2003, 08:29 PM
just try this:


//copied and pasted lots of this from phpdev's examples

$host="localhost";
$username="";
$password="";
$db="test_DB";
$link=mysql_connect($host,$username,$password)
or die ("could not connect to database, please check that mysql is in fact running");
mysql_select_db($db);

// as Jason said, the (*) will just select all fields in a table. i've
// selected them separately, don't ask me why

$sql="SELECT ID,Entry FROM Table1"; // the sql statement
$result=mysql_query($sql); // actually executing the query here

//now print out all the IDs and Entries in each row using a loop
for($i=0; $i<mysql_num_rows($result); $i++){
$row_array = mysql_fetch_row($result); //fetch current row
echo("ID: {$row_array[0]}, Entry: {$row_array[1]} <br>");
}

if you can't get the code by reading it, i'll assume it means that you really haven't done much work with databases/programming. you should get hold of some tutorials.
have you downloaded phpdev? it's got lots of elementary examples to get u up and running.
visit www.php.net and read a bit of the manual (http://www.php.net/manual/en/), www.phpfreaks.com, www.mysql.com.
there are lots more php tutorials (http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=php+tutorials+for+beginners) all over the web.

:thumbsup:

<edit>
oops.. a minute late... now if i'd seen spookster's reply first i wouldn't bother here. :rolleyes:
</edit>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum