PHP/MySQL echo links - php

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

Related

Query failed in php

$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

Unable to retrieve data from table?

Please consider the following code. I believe it's very straight forward however I couldn't see what's wrong with it.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT cif, br,
fullname, id, id_type,issuance_country,class
FROM ciftable';
mysql_select_db('ciftable');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "CIF :{$row['cif']} <br> ".
"BR : {$row['br']} <br> ".
"Full Name : {$row['fullname']} <br> ".
"ID : {$row['id']} <br> ".
"ID Type: {$row['id_type']} <br> ".
"Issuance Country : {$row['issuance_country']} <br> ".
"Class : {$row['class']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
This is the fresh installation of xampp so will be using default username and password. Kindly ignore all the possible SQL injection or best practices.
The above code throwing exception:
Could not get data: No database selected
Kindly advise.
Missing 2nd parameter:
$db1 = mysql_connect($dbhost, $dbuser, $dbpass);
$rv = mysql_select_db($dbname1, $db1);

Query was empty error | Trying to get a specific Mysql Value

Thanks in advance for any help you could bring, I'm really noob on all coding stuffs but this is the situation:
I have a wordpress website focused on Travel Agency Tours and hotels, and I'm working on an extranet system, the idea is that hotel managers will be able to change the price of their rooms, but this extranet works totally apart of wordpress, its a php script, with a different mysql database, so, what I would like to do is that when the price is changed in the extranet, then in wordpress this is going to be also changed in real time, so I'm trying in this way:
I'm planning to insert a php snippet on wordpress editor with a mysql code to get the price from the other database for each hotel and room, so, I'm going to do this for every hotel manually, the problem becomes from the code im trying to use to get that value:
<?php
$dbhost = 'localhost';
$dbuser = 'test_user';
$dbpass = 'pass';
$dbname = 'test_db';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = mysql_query("SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'");
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Room Price :{$row['room_price']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
So the result I get in my test.php file with this code is:
"Could not get data: Query was empty"
I'm not sure why is this error, and why is not printing the value I want.
PostData: I really appologize for my english since is not my language, but I'm trying to learn.
and try connection like this
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
You need to remove coma from your query after room price
//You are running the query twice so use this query
$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";
and learn mysqli or PDO as mysql are deprciated and soon going be drop
you have send the query two times...
try this
$sql = "SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'";
$retval = mysql_query( $sql, $conn );
<?php
$dbhost = 'localhost';
$dbuser = 'test_user';
$dbpass = 'pass';
$dbname = 'test_db';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$db_selected = mysql_select_db($dbname,$conn);
if (!$db_selected) {
die ('Can\'t use test_db : ' . mysql_error());
}
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Room Price :{$row['room_price']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
For select database use mysql_select_db() . you can't pass dbname as an args in mysql_connect()
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
Also if you are learner then learn mysqli or PDO
Note :- mysql_* has been deprecated
you are calling query run two times so change below lines
$sql = mysql_query("SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'");
$retval = mysql_query( $sql, $conn );
to
$retval = mysql_query("SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");

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

How to update many fields with single query?

I am working with a MLM company where com has to register the member, if it detects any mistake in data, it has to update the data of member again.
I have the right code which works well when all fields has to update but problem is it does not work for individual input boxes. Please give me solution of this problem.
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'vvvv';
$dbpass = 'xxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$usrid = $_POST['uid'];
$pwrd = $_POST['pwd'];
$nm = $_POST['noe'];
$fnm = $_POST['fn'];
$addrs = $_POST['adrs'];
$cntn = $_POST['cnt_no'];
$cty = $_POST['ct'];
$sql = "UPDATE office_user ".
"SET
password = '$pwrd',
name='$nm',
father_name='$fnm',
address='$addrs',
contact_no='$cntn',
city='$cty' ".
"WHERE user_id = '$usrid'" ;
mysql_select_db('my_db');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

Categories