How to check for data in a Mysql database using PHP? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
开发者_Go百科 Improve this questionI was wondering how can you check to see if certain data is present in a database and if so display it on the web page and if not don't display it using PHP.
You select the data and you can use mysql_num_rows() to check how many rows were returned.
If no rows are returned, you can do something else.
Rough unsafe example
$query = mysql_query("SELECT * FROM users WHERE id = 20");
$num = mysql_num_rows($query);
if($num > 0)
{
// Stuff to do when you find the items
}
else
{
// No items found
}
That's a pretty involved question. It requires you to connect to your database mysql_connect()
, query it mysql_query()
, and pour out any results from the query mysql_fetch_object()
.
Using the PHP MySQL interface functions, you run a SQL SELECT
query that will retrieve the data you are looking for. Using a PHP control structure such as if
, you then conditionally display the data, or not, depending on its existence or lack thereof.
精彩评论