Trying to get a result not displaying - php

I'm just trying to get something basic to appear for now and it's not working. It should display 1 on the screen. Is my logic wrong? I paste my statement in console and get 1.
<?php
$sql = mysqli_query("Select moist_measure_avail from sigh_in_account where moist_measure_avail = '1'");
$result = mysqli_fetch_array($sql);
echo $result['moist_measure_avail'];

First of all you should have to create a connection
<?php
$conn = mysqli_connect("localhost "," root","","dbname");
?>
And then you have to include the conn variable in your query
$sql = mysqli_query( $conn, " SELECT moist_measure_avail from sigh_in_account WHERE moist_measure_avail = '1'");
The sql statements must be either all caps or all small

$sql = mysqli_query("Select moist_measure_avail from sigh_in_account where moist_measure_avail = '1'");
pass connection as first param in above method. something like this
$sql = mysqli_query($conn,"Select moist_measure_avail from sigh_in_account where moist_measure_avail = '1'");
where $conn is mysql connection

Related

mysql database keeps returning null

if ($con->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Accounts WHERE acccount_num ='$accountNum'";
$result = $con->query($sql);
//$row = $result->fetch_row();
$row = mysqli_fetch_assoc($result);
print (json_encode($row));
$con->close();
This is my php code for connecting to the database where it is failing. I checked the field names in the actual database and I also checked all my code in the java project where my code is actually using the database. Really new to the whole concept of databases and PHP.
Use this
$sql = "SELECT * FROM Accounts WHERE acccount_num =$accountNum";
instead of
$sql = "SELECT * FROM Accounts WHERE acccount_num ='$accountNum'";
Try this or maybe you have an extra c in acccount_num.
$sql = "SELECT * FROM `accounts` WHERE `acccount_num` ='".$accountNum."'"

Need Some Help Regarding Fetching Data from Mysql using explode function

In the below script I want to fetch data from mysql using a explode function and also a variable within an explode function.
Here's how I want to get
<?php
include ('config.php');
$track = "1,2,3";
$i = 1
$trackcount = explode(",",$track);
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
This is the code
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
I want sql to fetch data from tracks table where id = $trackcount[$i]
Whatever the value of $trackcount[$i] mysql should fetch but it shows a blank screen.
If I put this
$sql = "SELECT * FROM tracks WHERE id='$trackcount[1]'";
It works perfectly
save your $trackcount[$i] in one variable and then pass it in the query as given below
<?php
include ('config.php');
$track = "1,2,3";
$i = 1;
$trackcount = explode(",",$track);
$id=$trackcount[$i];
$sql = "SELECT * FROM tracks WHERE id='$id'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
and one more thing check your previous code with echo of your query and see what is passing ok.
echo $sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//like this
problem is with your query
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//change
to
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
Generally you would want to use the IN operator with this type of query, so for you this would be:-
$sql="SELECT * FROM `tracks` WHERE `id` in (".$track.");";
or, if the $ids are in an array,
$sql="SELECT * FROM `tracks` WHERE `id` in (".implode( ',', $array ) .");";

Displaying a SQL table data for a specific user

I want to display the table participantes with the columns sorteo, nombre and fecha.
The user info is on another table sellify_users (usern column).
I want to display only that user data using:
SELECT * FROM participantes WHERE nombre = 'usern'
But usern is not in the same table, so if possible I want to call the sellify_users to get the usern data.
<?php
$user = 'database_user';
$password = 'database_pass';
$database="database_name";
mysql_connect(localhost,$user, $password);
#mysql_select_db($database) or die( "Unable to select database");
echo $query = "SELECT * FROM participantes WHERE nombre='usern'";
$result = mysql_query($query);
mysql_close();
?>
If you meant that a user has a record in the table sellify_users and you need to find the usern from it in order to use it in the next query:
$query = "SELECT * FROM participantes WHERE nombre='usern'";
Then all you need to do is to run a query for the table sellify_users first and get the value usern from it, store it in a variable and then use that in your second query. Something like:
$query1 = "SELECT * FROM sellify_users";
$result1 = mysqli_query($con, $query1);
$row1 = mysqli_fetch_assoc($result1);
$usern = $row1['usern'];
$query2 = "SELECT * FROM participantes WHERE nombre='usern'";
$result2 = mysqli_query($con, $query2);
while($row2 = mysqli_fetch_assoc($result2)){
echo $row2['ColumnNameHere1'];
echo $row2['ColumnNameHere2'];
}
Notice: I've passed $con which denotes a connection variable, i.e.
you should read about mysqli or PDO and if there's anything you do not understand, feel free to shoot a query.
EDIT:
If by any chance you are trying to use the last inserted record, you should look for mysqli_insert_id($con); and use that instead.

Warning: mysql_error() expects parameter 1 to be resource, string given

<?php
session_start();
$username = "root";
$password = "password";
$database = "meipolytechnic";
mysql_connect('localhost', $username,$password);
#mysql_select_db($database) or die(mysql_error());
$username=$_SESSION['MM_Username'];
$query = "SELECT rollno FROM users where username = '".$username."'";
$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);
mysql_close();
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
echo ($rows['rollno']);
?>
i want to retrieve only the logged in users roll no from users table in database
when i run this code
and log in as foo
i get the following stuff
Unknown column 'foo' in 'where clause'
There should be session_start() at the top of the page
query need to change as
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."' ";
EDIT
Please try something before posting a question here. Please google or go through www.w3school.com for clearing this kind of issues. Make a good knowledge about arrays and mysql connection. And mysql_query function won't work latest PHP version.
Please try following code.
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
To use an array inside a string you need to put a curly bracket before it and after it
so
$query = "SELECT rollno FROM users where username = {$_SESSION['MM_Username']}";
or
$query = "SELECT rollno FROM users where username = ".$_SESSION['MM_Username'];
First of all start session using start_session()
then change your query:
$query = "SELECT rollno FROM users where username = ".$_SESSION['MM_Username'];
then change:
$num = mysql_num_rows($result); instead of $num = mysql_numrows($result);
Try this query
$query = "SELECT rollno FROM users where username = ".$_SESSION[MM_Username]." ";
And start session on same page.
You can use
$username=$_SESSION[MM_Username];
$query = "SELECT rollno FROM users where username = '".$username."'";
and start the session by using session_start()
and you have used two closing tags omit one make it like below
echo ($rows['rollno']);
?>
This type of error occur when query goes false
May be becouse You have not start session, becouse if you dont have session_start(), then nothing will come in session variable... just try as
$query = "SELECT rollno FROM users where username = '".$_SESSION[MM_Username]."'";
may this help you
You must need to use session_start() before using $_SESSION variable in code.
So put below code at start,
session_start();
Then do some modification in query like,
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
first start your session
session_start();
and Change your query like this...
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";

How to use multiple database using php?

I have read multiple question in the internet including this stackoverflow question but none of them working for me. Here is my code:
<?php
$conn1 = mysql_connect("localhost","root","passw0rd") or die(mysql_error());
$conn2 = mysql_connect("localhost","root","passw0rd") or die(mysql_error());
mysql_select_db("asteriskcdrdb",$conn1);
mysql_select_db("pj8v2",$conn2);
$query = "SELECT * FROM cdr";
$result = mysql_query($query,$conn1);
var_dump($result);
$query2 = "SELECT * FROM tb_did_avalaible";
$result2 = mysql_query($query2,$conn2);
var_dump($result2);
?>
When i var_dump the result, it return false. What is the problem here? Thank you.
You dont need two connections, if both databases are located on the same mysql-server and you access them both as unique user.
You also don't need to select a DB.
Just use the database-name as prefix when specifying the tables:
<?php
mysql_connect("localhost","root","pass") or die(mysql_error());
$query = "SELECT * FROM asteriskcdrdb.cdr";
$result = mysql_query($query)or die(mysql_error());
var_dump($result);
$query2 = "SELECT * FROM pj8v2.tb_did_avalaible";
$result2 = mysql_query($query2)or die(mysql_error());
var_dump($result2);
?>
The real problem in your code is: there can only be one active DB, it should work this way:
<?php
$conn1 = mysql_connect("localhost","root","passw0rd") or die(mysql_error());
$conn2 = mysql_connect("localhost","root","passw0rd",true) or die(mysql_error());
mysql_select_db("asteriskcdrdb",$conn1);
$query = "SELECT * FROM cdr";
$result = mysql_query($query,$conn1);
var_dump($result);
mysql_select_db("pj8v2",$conn2);
$query2 = "SELECT * FROM tb_did_avalaible";
$result2 = mysql_query($query2,$conn2);
var_dump($result2);
?>
Altough there's no need for 2 connections, you can select both DB's using the same connection.
Sorry i just figure out the problem. If using same connection parameter, must add true in the connect parameter
$conn1 = mysql_connect("localhost","root","passw0rd") or die(mysql_error());
$conn2 = mysql_connect("localhost","root","passw0rd",true) or die(mysql_error());
Don't use mysql connector, use mysqli. It is more secure compared to mysql.
the code would be.
$conn1 = new mysqli("localhost","user","password","db1");
$conn2 = new mysqli("localhost","user","password","db2");
$query1 = "select * from table1";
$query2 = "select * from table2";
echo $query1 . "<br />";
echo $query2 . "<br />";
$rs1 = $conn1->query($query1);
$rs2 = $conn2->query($query1);
Also check if the the query is correct. Most of the times the error is in the query and not the syntax.

Categories