Retrieving text from a mysql database - php

I am sure this is a stupid question but I can't figure out how to do it. I need to retrieve a large block of text that is stored in a database. I know how to connect to the database and submit the sql query. What I can't figure out how to do is have it store that text in a string so I can have it echo later on the page.
What I have so far
$db_name = "j_db";
$table_name = "contacts";
$connection = #mysql_connect("localhost", "*****", "****") or die(mysql_error());
$db = #mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT meaning
FROM $table_name
WHERE card = (php variable here)";
$result = #mysql_query($sql, $connection) or die(mysql_error());

Using mysql_fetch_array()
while( $row = mysql_fetch_array($result) ){
$meaning = $row["meaning"];
echo $meaning;
}

Look at the mysql_fetch_* functions. mysql_fetch_assoc is a popular choice.

<?php
$url= '' ;
$username = '' ;
$password = '' ;
$dbname = '' ;
$connection = mysql_connect('$url' , '$username', '$password');
mysql_select_db('$dbname' , $connection);
$result = mysql_query("SELECT textColumnName FROM tableName", $connection);
while($row = mysql_fetch_array($result)){
echo $row['textColumnName'];
}
?>

Related

Delete From a Database Using Session ($_SESSION)

I have a record i want to remove from the database. I have so far been able save to the mysql database now i have several information in several rows, now suppose the information isnt the needed one, i want to delete it from the database. Thats what i am trying to achieve here
I tried this
<?php
session_start();
require_once('inc/config.php');
if(!isset($_SESSION['username'])){
header('Location: signon.php');
}
?>
<?php
require_once('inc/config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot connect: '.mysqli_error());
$sql = "SELECT * FROM education_info WHERE username = '" . $_SESSION['username'] . "'";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
$id = $row['id'];
$username = $row['username'];
$sql2 = "DELETE FROM education_info WHERE id = $id" ;
$result = mysqli_query($con,$sql);
mysqli_close($con);
header("Refresh:0; url=EDWE.php");
?>
Only that the information still remains present in the database, How do i go about deleting it completely, if not needed?
You are passing wrong variable while executing delete query:
$sql2 = "DELETE FROM education_info WHERE id = $id" ;
$result = mysqli_query($con,$sql2); //<---pass $sql2

to retrieve a mysql data in php and echo the retrieved data

<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
mysql_close();
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
?>
This is my code i want to display the roll number of the currently logged in user and
when i run this code i get no database selected.
First of all, you should code properly, means database connection and database selection should be on top:
<?php
$username = "root";
$password = "password";
$database = "xxxx";
$link = mysql_connect("localhost", $username, $password);
mysql_select_db('xxxx', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
Also moved mysql_close(); on last
One other main point was, now mysql_ is deprecated, please use mysqli_
Remove this statement from line number 10
mysql_close();
just remove these two lines before while that will solve ur problem
$result = mysql_query($query) or die(mysql_error());
$rows = array();
Use this code and use mysql_close function in the last.
<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db($database, $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
You have closed the connection from MySQL before the mysql_query mysql_close();
try this
<?php
$username = "root";
$password = "password";
$database = "dfsdftwsdgdfgdfsgsdf";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
mysql_close();
print_r($rows);
?>
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
The order should be like this
mysql_select_db('meipolytechnic', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('meipolytechnic', $link);

No database selected on PHP

I got this code to create a JSON file with the data I have in my database:
<?php
$connect = mysql_connect("localhost", "root", "123", "boxr") or die("Error connecting with the host.");
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
$response = array();
$users = array();
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$gamertag = $row['gamertag'];
$banned = $row['banned'];
$users[] = array('gamertag' => $gamertag, 'banned' => $banned);
}
$response['users'] = $users;
$fp = fopen('users.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
?>
The problem is when I run the script it shows the following:
No database selected
I checked my code twice but I can't figure out why its telling me that.
You have to select the database name with:
mysql_select_db ('your_db_name');
Well you can do it like that:
$connect = mysql_connect("localhost", "root", "123") or die("Error connecting with the host.");
mysql_select_db("boxr", $connect);
And later, correct it:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($result))...
By:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($sql))...
If you are calling the function with the name $sql, you can't later put the name of $result.
mysql_* functions are deprecated but you could change your query
$sql = mysql_query("SELECT * FROM dbname.users") or die(mysql_error());
Use :
$db_selected = mysql_select_db('db_name', $connect);
if (!$db_selected) {
die ('Can\'t use db_name : ' . mysql_error());
}

MySQL query data into a PHP variable

I want to get a value from a MySQL database and put it into a PHP variable.
I tried this:
$data = mysql_query("SELECT userid FROM ao_user " .
"WHERE username = '{$this->_username}' " .
"AND password = '{$this->_password}' AND display = '{$this->_display}'");
The code says invalid username/password.
Here is the user login code:
<?php
$username = "Nynex71";
mysql_connect("localhost", "root", "test") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$result = mysql_query("SELECT display FROM ao_user " .
"WHERE username = '{$username}'") or die(msyql_error());
$row = mysql_fetch_assoc($result);
echo $row['display'];
?>
and
public function getDisplay()
{
mysql_connect("localhost", "root", "test") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$result = mysql_query("SELECT display FROM ao_user " .
"WHERE username = '{$this->_username}'");
$row = mysql_fetch_assoc($result);
$this->_display = $row['display'];
$_SESSION['display'] = $this->_display;
}
The program does not put any words into the PHP variable. What am I doing wrong and how do you do this?
mysql_query returns a result handle, not the value you selected. you have to first fetch a row, then retrieve the value from that row:
$result = mysql_query("SELECT ...") or die(msyql_error());
$row = mysql_fetch_assoc($result);
echo $row['userid'];

PHP, MySQL table query syntax error?

I hope someone can help see what's wrong here:
I have a form with two field EMAIL and PASSWORD that opens a php page where I intend to run a simple query on a table.
I get an error message that makes no sense:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#gmail.com' at line 1.
The email address I entered in this case did end with '#gmail.com'
Here's the code:
<?php
$dbhost = 'somewhere.net';
$dbuser = 'someUser';
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'medreunten_db1';
mysql_select_db($dbname) or die(mysql_error($conn));
$email = mysql_real_escape_string($_POST['email']);
$query = "SELECT * FROM employee WHERE email = $email";
$result = mysql_query($query, $conn) or die (mysql_error($conn));
extract(mysql_fetch_assoc($result));
while ($row = mysql_fetch_array($result)) {
extract($row);
echo $row['name'];
echo $row['surname'];
echo $row['age'];
}
?>
Any advice would be appreciated.
You are missing quotes around string fields:
$query = "SELECT * FROM employee WHERE email = '$email'";
Additionally,
extract(mysql_fetch_assoc($result));
will fetch the first row from the database, so your while loop will start from the second row.
You have to put the value in quotes inside SQL string.
$email = mysql_real_escape_string($_POST['email']);
$query = "SELECT * FROM employee WHERE email = '$email'";
(mind the extra '' around $email)
Your query translates to:
SELECT * FROM emloyee WHERE email = foo#bar.com
This doesn't work, you have to put strings in quotes. Change your code to the following and it will work:
$query = "SELECT * FROM employee WHERE email = '$email'";
Just single quote the variable '$email' because it varchar type value and field .
As wrote, Darhazer :)
Full fixed code:
<?php
$dbhost = 'somewhere.net';
$dbuser = 'someUser';
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'medreunten_db1';
mysql_select_db($dbname) or die(mysql_error($conn));
$email = mysql_real_escape_string($_POST['email']);
$query = "SELECT * FROM employee WHERE email = '$email'";
$result = mysql_query($query, $conn) or die (mysql_error($conn));
extract(mysql_fetch_assoc($result));
while ($row = mysql_fetch_array($result)) {
extract($row);
echo $row['name'];
echo $row['surname'];
echo $row['age'];
}
?>

Categories