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'];
Related
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
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());
}
function DisplayPoints()
{
$con = mysql_connect("localhost", "grame2_admin", "password") ;
if (!$con) {
die("Can not connected: " . mysql_error());
}
mysql_select_db("grame2_webpage",$con);
$sql = "SELECT points FROM tablename WHERE username = $username";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
It echo out info, that there is error in line 164, LINE ABOVE THIS.
echo $record['points'] ;
}
mysql_close($con);
}
the main idea is to echo out INT from specific user in webpage. Please help! :)
Change
$sql = "SELECT points FROM tablename WHERE username = $username";
to
$sql = "SELECT points FROM tablename WHERE username = '$username'";
^ ^
On a side note use prepared statements with either mysqli or PDO. mysql extension is deprecated and is no longer supported.
UPDATE The other problem is that your $username variable is not initialized. You probably need to pass it to your function as a parameter
function DisplayPoints($username)
{
...
}
And when you call your function pass a value
DisplayPoints('user1');
or
$username = $_POST['username'];
// here should go code to validate and sanitize $username
DisplayPoints($username);
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
This is the code for attempting to do a update on mysql data errors stating undefined variable
mysql_connect ("localhost", "root", "");
mysql_select_db("supplierdetails");
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Run a query
$result = mysql_query ("SELECT * FROM users WHERE id= '$id'");
while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
$password=$row['password'];
}
$query = "UPDATE users SET username = '$username', password = '$password' WHERE id = '$id'";
$result = #mysql_query($query);
//Check whether the query was successful or not
if($result) {
header("message= Users Updated");
}else {
die("Query failed");
}
?>
You miss the $id value?
And can use echo to debug or check script result, not header
http://php.net/manual/en/function.header.php
Please be more specific with regards to which variable is undefined.
In the code you've posted $username and $password are only set if $result returns a result, if it doesn't then your while loop will not run and therefore $username and $password will never be set.
Also $id doesn't look as if that has been set either, unless this has been set outside of the code which you have included in your question.
Hope this helps :)
you used 2 connect no need to do while and you forgot $id
$con = mysql_connect("localhost", "root", "");
mysql_select_db("supplierdetails");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$id = $_POST['id'];
$username=$_POST['username'];
$password=$_POST['password'];
$query = "UPDATE users SET username = '".$username."', password = '".$password."' WHERE id = '".$id."'";
$result = mysql_query($query);
//Check whether the query was successful or not
if($result) {
echo "message= Users Updated";
}else {
die("Query failed");
}
?>
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'];
}
?>