How to enter a server IP into a mysql table? - php

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]')

Related

PHP/MySQL echo links

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);
?>

Having issues with MySQL and PHP

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";
}

syntax error when delete sql data using php

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" ;

Mysqli wont allow table's to show when called on

As of recently ive been learning php and at that conjuntion in between where i have to now use Mysql in order to keep my bigger info table ogranized, well i wrote this code in order to show the tables (or so i think i did it right). im completely stumped because i can not see any of the displaying tables that i am calling on and the more ive tried the less i works so i was wondering if anyone can see a loop hole in my code or maybe im doing something wrong? or maybe everything ive done is wrong...?
`
$dbhost = "localhost";
$dbuser = "juliegri_AAlassa";
$dbpass = "********"; // to not show real password
$dbname = "juliegri_AAlassaly";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno () . ")"
);
}
?>
<?php
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed");
}
?>
<!doctype html>
<html lang="en">
<head>
<title>databases</title>
</head>
<body>
<ul>
<?php
while($subject = mysqli_fetch_assoc($result)) {
?>
<li><?php echo $subject["menu_name"] . "(" . $subject["id"] . ")"; ?></li>
<?php
}
?>
</ul>
<?php
mysqli_free_result($result);
?>
</body>
</html>
<?php
mysqli_close($connection);
?>`
Have you forgotten the opening PHP tag at the beginning of your page?
<?php
$dbhost = "localhost";
$dbuser = "juliegri_AAlassa";
$dbpass = "********"; // to not show real password
$dbname = "juliegri_AAlassaly";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno () . ")"
);
}
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed");
}
?>
Two things i think could be wrong.
Here is a correct implementation to compare. It could be the first PHP opening tag, i also added the default port to the connect statement, and added some try catches with error messages, these can tell if the connect or query is not working.
<?php
$dbhost = "localhost";
$dbuser = "juliegri_AAlassa";
$dbpass = "********"; // to not show real password
$dbname = "juliegri_AAlassaly";
//original connect statement with a port added in
try {
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname , 3306);
} catch(Exception $e) { echo $e->getMessage(); }
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//Query looks fine, easier to trouble shoot when its one line, first get it working then break it up
$query = "SELECT * FROM subjects WHERE visible = 1 ORDER BY position ASC";
// This will try to fetch the result and give an error if it can't.
try { $result = mysqli_query($connection, $query);
} catch(Exception $e) { echo $e->getMessage(); }
if (!$result) { die("Database query failed"); }
?>
Is it alright if I alter some of your codes?
See this:
<!doctype html>
<html lang="en">
<head>
<title>databases</title>
</head>
<body>
<?php
/* ESTABLISH CONNECTION */
$connection=mysqli_connect("localhost","juliegri_AAlassa","YourPassword","juliegri_Aalassaly");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
/* START QUERY */
$result=mysqli_query($connection,"SELECT * FROM subjects WHERE visible='1' ORDER BY position ASC");
?>
<ul>
<?php
/* DO THE WHILE LOOP */
while($subject = mysqli_fetch_array($result)) {
?>
<li><?php echo $subject['menu_name'] . "(" . $subject['id'] . ")"; ?></li>
<?php
} /* END OF WHILE LOOP */
?>
</ul>
</body>
</html>

MySQL Syntax for Where clause

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";
}

Categories