简体   繁体   中英

Return id of row of found value in MySQL and PHP

Hey guys, got some of this code written, just having trouble with the bit of MySQl that returns the found value's row and subsequently a value in a column in that row. So heres what I have

<?php
//mysql deets
$user="x";
$password="x";
$database="x";
$host="db285.x";

//open our db for check
mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$searchterm = "Dex";
$dbsearchterm = mysql_real_escape_string($searchterm); 
$query = "SELECT * FROM contacts WHERE name='$dbsearchterm';"; 
$res = mysql_query($query); 

if (mysql_num_rows($res) > 0) { 
    echo "found ".$searchterm."and the row it is in is ".$rownum." and value of the number column in row is ".$number;

} 
else {

    echo 'no find';

} 
mysql_close();
?>

So I can find if the name 'dex' is in there, but how can I return and set the row id and value in the row's 'number' column?

Thanks guys, Dex

Question isn't very clear but it seems like you want to display the values from the database. If that's the case then this should work, if this is not what you were after please clarify your question a little better.

while ($row = mysql_fetch_assoc($res)) {
echo "found ".$searchterm."and the row it is in is ".$row['id']." and value of the number column in row is ".$row['number'];
}

You haven't posted your database fields, so replace the keys, $row['id'] and $row['number'] with the fields from your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM