Im not good in php. I have a web application that displays the Data from my MySQL Database. I am not getting any erros but I need to fetch the Data I want.
ex. code.
<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$token = $_GET['token'];
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM table WHERE token = '" . $token . "'";
mysql_select_db('database');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//Displays the data
echo "The Token is here";
}
mysql_close($conn);
?>
I have no errors in here. But I need to do something like, If the token data is not yet added to the table, then I will put an echo message like.. "The token is not yet added here". Where I can put the syntax here? Please help me. I am new in php.
Use this:
if(mysql_num_rows($retval)) {
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//Displays the data
echo "The Token is here";
}
} else {
echo "The token is not yet added here";
}
Try with mysql_num_rows
like
$sql = "SELECT * FROM table WHERE token = '" . $token . "'";
mysql_select_db('database');
$retval = mysql_query( $sql, $conn );
$num_rows = mysql_num_rows($retval);
if($num_rows < 1) {
echo "The token is not yet added here";
} else {
echo "Token already added";
}
And try to avoid using mysql_*
functions due to they are deprecated.Instead use mysqli_*
functions or PDO
statements.
Put condition with mysql_num_rows(). If that value is 0 then you can show the message The token is not yet added here
else some other codes.
Try if the following way:
if(isset($_GET['token']))
{
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM table WHERE token = '" . $_GET['token'] . "'";
mysql_select_db('database');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//Displays the data
echo "The Token is here";
}
mysql_close($conn);
}
else
{
echo "The token is not yet added here";
}
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.