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";
}
Related
I want to create a page which displays no of times visited and all the IP addresses visiting the web page.
For that I created a mysql table which displays new IP in a new row, but I am unable to execute it.
Most likely, the error is in entering server ip into the table.Here's the full code:
<html>
<head>
<title>Delta sys ad task3</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'prabakar';
$dbpass = 'praba1110';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('server_IPs', $conn);
echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
$counter=1;
$flag=0;
$sql='SELECT * FROM IPs';
$retval = mysql_query( $sql, $conn );
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
if($_SERVER[SERVER_ADDR]==$row['ip'])
break;
else
$flag=1;
}
if($flag==1)
{
$sql="INSERT INTO IPs VALUES('$_SERVER[SERVER_ADDR]')";
$retval = mysql_query( $sql, $conn );
}
echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
$counter++;
print "No of times site visited: $counter
IP Adresses visited:";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
print "$row['ip']";
}
mysql_close($conn);
?>
</body>
</html>
You need to remove the extra quote:
Change
('$_SERVER['SERVER_ADDR']')
to
('$_SERVER[SERVER_ADDR]')
$query = "SELECT * FROM Student WHERE student_id = $child AND school_id = $college";
$result = mysqli_query($conic, $query);
if(!$result){
die(" query failed");
echo 'error here';
} else {
i am trying to retrieve some information from the SQL database but the query seems to fail any ideas?
You must learn more : how to retrieve data from database using select in php
so try out following example to display all the records from Student table
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM student WHERE student_id = '"$child"' AND school_id = '"$college"' ";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
// some code here
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
NOTE
The mysql extension is deprecated and will be removed in the future:
use mysqli or PDO
I've looked all over Google for some examples of this, but I just can't seem to find a solution! Either they're going for an internal URL (with a dynamic ID pointing to a page, etc.) Basically all the other questions are about links that are more complex than what I'm going for.
Basically I have a table with 2 fields - name, and URL. (the page is a secret santa page where users can share their Amazon wish lists and view other users lists)
I want the URL to echo a link to the proper amazon wishlist URL.. here's my code:
<?php
$dbhost = 'localhost:post';
$dbuser = 'db_user';
$dbpass = 'db_pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT name, url FROM santa';
mysql_select_db('rev_phoenix');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "<b>Username:</b> {$row['name']} <br> ".
"<b>Wishlist:</b> {$row['url']} <br> ".
"<hr>";
}
echo "Merry Christmas!\n";
mysql_close($conn);
?>
As far as I'm aware, mysql has been depricated as of PHP 5.5.0
You may want to go with using mysqli
Try using this for your code:
<?php
$dbhost = 'localhost:post';
$dbuser = 'db_user';
$dbpass = 'db_pass';
$dbcurrent = 'rev_phoenix';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbcurrent);
if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}
//Added order by random, and limit 1 to only show 1 'secret' santa.
$sql = 'SELECT name, url FROM santa ORDER BY RAND() LIMIT 1';
$retval = mysqli_query( $sql, $conn );
if(! $retval ) {
die( 'Could not get data: ' . mysqli_error($conn) );
}
while($row = mysqli_fetch_assoc($retval) {
echo "<b>Username:</b> ". $row['name'] . "<br> ".
"<b>Wishlist:</b> <a href='" . $row['url'] . "'>List</a><br>".
"<hr>";
}
echo "Merry Christmas!\n";
mysqli_close($conn);
?>
I'm trying to display information from a MySQL database with PHP. When I don't have the PHP inside the HTML, everything appears normally (albeit without data) as seen here: http://i.imgur.com/SrqASpu.png (sorry about the links, I can't post images yet as I don't have 10 rep)
However, when I put in the PHP for water temperature, the page doesn't load past that panel as seen here: http://i.imgur.com/qknHFPp.png
This is what the code looks like:
<div class="panel-heading">Water Temperature</div>
<div class="panel-body">
<h1>
<?php
$dbhost = 'I have the IP for the host here';
$dbuser = 'then my username';
$dbpass = 'then finally the password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT waterTemp FROM updates';
mysql_select_db('mason');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['waterTemp']}° C";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
</h1>
</div>
Any assistance would be greatly appreciated :)
Try This:
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo $row['waterTemp']."° C";
}
that one helpful for you.
$sql = 'SELECT * FROM updates';
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$waterTemp = $row['waterTemp'];
echo "{$waterTemp}° C";
// or echo "{". $waterTemp . "}° C";
}
im getting the following syntax error can someone please help!
im guessing it something soooo easy but i have been looking at it for ages and can see what im doing wrong
<?php
if(isset($_POST['delete']))
{
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$CourseId = $_POST['CourseId'];
$sql = "DELETE course ".
" WHERE CourseId = $CourseId" ;
mysql_select_db('d11os_projectdb');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
}
else
{
?>`enter code here`
$sql = "DELETE FROM course ". --<-- Missing key word FROM
" WHERE CourseId = $CourseId"
You are missing the table name from the sql query
$sql = "DELETE course FROM **table_name**".
" WHERE CourseId = $CourseId" ;