Mysqli Query Echo result - php

Hi I am trying to make status of user so it will check how many post this user has and echo out the result, it was working fine when I was using mysql but after converting it to mysqli it giving me some errors
Here is my code:
<?php
if(!isset($_COOKIE['loggedin'])){
header("location:index.php");
}
session_start();
if(!isset($_SESSION['user'])){
header("location: index.php");
}
else {
?>
<?php
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$resule = "SELECT count(ID) from save_data where username = '".$_SESSION['user']."'"
or die(mysql_error());
$result = $con->query($resule);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0], $row[1];
?>
<?php }?>
Error:
Notice: Undefined offset: 1 in C:\xampp\htdocs\mysql_login\status.php on line 30

Try removing $row[1] from:
echo $row[0], $row[1];

As the error says the offset 1 is undefined. so its pretty simple that you should remove $row[1] from echo $row[0], $row[1];

The problem is $row[1] in echo $row[0], $row[1]; because the $result->fetch_array will only fill the $row[0], because the SQL Query
"SELECT count(ID) from save_data where username =
'".$_SESSION['user']."'"
will only return one result if username is unique.

What will be happened If your query doesn't return any values? So better you check
if($resesult)
{
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];// remove $row[1] because it fills only the $row[0]
}

Related

How to insert data from a select query using php

I wan to insert data to mysql table from another database which is connected via ODBC.But I cannot enter into the while loop, here is my code -
N.B: For security I dont provide db name, user and pass.
ODBC connection declared as 'connStr'
<?php
$connStr = odbc_connect("database","user","pass");
$conn = mysqli_connect("server","user","pass","database");
//$result_set=mysqli_query($conn,$datequery);
//$row=mysqli_fetch_array($result_set);
echo "<br>";
echo "<br>";
$query="select cardnumber, peoplename, creditlimit, ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'" ;
$rs=odbc_exec($connStr,$query);
$i = 1;
while(odbc_fetch_row($rs))
{ //echo "Test while";
$cardnumber=odbc_result($rs, "cardnumber");
$peoplename=odbc_result($rs, "peoplename");
$creditlimit=odbc_result($rs, "creditlimit");
$cbalance=odbc_result($rs, "cbalance");
$minpay=odbc_result($rs, "minpay");
$conn = mysqli_connect("server","user","pass","database");
$sql= "INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay) VALUES ('cardnumber', 'peoplename', 'creditlimit', 'cbalance', 'minpay') ";
if(!(mysqli_query($conn,$sql))){
//echo "Data Not Found";
echo "<br>";
}
else{
echo "Data Inserted";
echo "<br>";
}
echo $i++ ;
}
echo "<br>";
odbc_close($connStr);
?>
How can I solve this?
If you really want to understand why you can't enter while() {...}, you need to consider the following.
First, your call to odbc_connect(), which expects database source name, username and password for first, second and third parameter. It should be something like this (DSN-less connection):
<?php
...
$connStr = odbc_connect("Driver={MySQL ODBC 8.0 Driver};Server=server;Database=database;", "user", "pass");
if (!$connStr) {
echo 'Connection error';
exit;
}
...
?>
Second, check for errors after odbc_exec():
<?php
...
$rs = odbc_exec($connStr, $query);
if (!$rs) {
echo 'Exec error';
exit;
}
...
?>
you can do it with just SQL check it here, like:
INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay)
SELECT cardnumber, peoplename, creditlimit,
ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'

Displaying fields in php from MS Access database

I have a simple studentinfo.accdb. I was able to connect to this database using php. Moreover, the values of the 'ID' column(primary key) are also being displayed. however, the field values are not. The error message is:
Connected
ID : 1
Warning: odbc_result(): Field class not found in
C:\xampp\htdocs\connect\index.php on line 20
The following is my code:-
<?php
$con=odbc_connect("studentinfo", "", "");
if($con)
{
echo "Connected<br>";
}
else
{
echo "Failed";
}
$sql="select * from Table1 WHERE ID = 1";
$result=odbc_exec($con, $sql);
while ($row=odbc_fetch_array($result)) {
echo "ID : ". $row['ID'];
if(isset($_GET['tName'])){
echo "NAME : ". $row['tName'];}
$asd = odbc_result($result, "class");
echo $asd;
// echo "CLASS :".$row['class'];
echo "<br/>";
}
?>
I have used an isset() and clearly the fileds are not set for some reason. Please help!

Checking whether username exists in MySQLi Database and PHP

I have been working on a website which has a xampp server and a database called users with a table called AccountDetails. About a year ago I got it to work perfectly, but the server I was using then required MySQL not MySQLi. Now I have to use MySQLi and can't even get the simplest of sql's SELECT function to work, any ideas would be much appreciated.
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$result = $link->query("SELECT ID, UserName FROM AccountDetails");
return $result->result();
var_dump($result);
mysqli_close($link);
?>
The Query itself works when I plug it into the phpmyadmin SQL section and it returns the values that I expect it too.
I've spent days looking online for different answers but none of them work, and the var_dump only gives me "bool(false)" which I don't think I should be getting.
You can try this code
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$sql_select = "SELECT * FROM AccountDetails";
$result = $link->query($sql_select);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "UserName: " . $row['UserName']. "<br>";
}
} else {
echo "No Records";
}
$link->close();
?>

PhP session passing and combine variable and string

My login code:
<?php
session_start();
$f_usr= $_POST["userid"];
$f_pswd= $_POST["password"];
$_SESSION['user']=$f_usr;
$con=mysql_connect("localhost","root","");
if(! $con)
{
die('Connection Failed'.mysql_error());
}
mysql_select_db("finaltest",$con);
$result=mysql_query("select * from user");
while($row=mysql_fetch_array($result))
{
if($row["username"]==$f_usr && $row["password"]==$f_pswd)
header('Location: selectdata.php');
else
echo"Sorry : $f_usr";
}
?>
selectdata.php
<?php
session_start();
$s= $_SESSION['user'];
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("finaltest") or die(mysql_error());
$select="temperature".$s;
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM $select")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>username</th> <th>password</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['username'];
echo "</td><td>";
echo $row['password'];
echo "</td></tr>";
}
echo "</table>";
?>
actually the session varibale is not getting parsed i am getting an error:
Notice: Undefined index: user in C:\xampp\htdocs\bars\selectdata.php on line 3
and i have an other problem i want to select a database named "temperaturexyz" where temperature i want to store in a string and xyz is variable that i am getting via the session i want to combine the both so that i can get a variable which i can use in the query
Regarding your session: It appears your session variable $_SESSION['user'] which you attempt to set in your login code is not correctly setting the variable as expected.
try:
<?php
session_start();
$f_usr= $_POST["userid"];
$f_pswd= $_POST["password"];
$_SESSION['user']=$f_usr;
echo $_SESSION['user'] //<-------- see if this echo's out the value you are expecting
As for your variable:
I assume you mean you want a variable name $temperaturexyz but you are creating the variable name dynamically. so
$select="temperature".$s;
$$select = /* Insert whatever value you need here*/ //<-- you can then call this variable like this (ie. $temperaturexyz)

MySQL - mysql_fetch_assoc() doesn't give one column

My column type is SMALLINT and name is user_level.
Here is the code I am using:
<?php
mysql_connect("localhost", "root", "*********");
mysql_select_db("3591_other");
$result = mysql_query("SELECT * FROM rivase_f_users WHERE user_name = '$email' AND user_pass=sha1('$passwd')");
if (!$result) {
echo mysql_error();
}
//snip
else if (mysql_fetch_assoc($result)) {
$row=mysql_fetch_assoc($result);
$_SESSION['username']=$email;
if ($row['user_level']==1) {$_SESSION['usertype']='leader';}
header("Location: index.php");
die();
}
?>
$row['user_level'] looks like to be null, but phpmyadmin says it is 1. I tried echoing it with commenting the header-location row, it did not say anything. What am I doing wrong?
try this,
else if ($row = mysql_fetch_assoc($result)) {
$_SESSION['username']=$email;
if ($row['user_level']==1) {$_SESSION['usertype']='leader';}
header("Location: index.php");
die();
}
What you're trying to accomplish is unclear to me but you are discarding the first result:
else if (mysql_fetch_assoc($result)) {
// ^^^^^^^^^^^^^^^^^ Read and discard
$row=mysql_fetch_assoc($result);
// ^^^^^^^^^^^^^^^^^ Read next row
You are experiencing a logical if-else bug.
You are reading the value of next mysql_fetch_assoc() after testing the output result of the first call in the else condition, but you wanted to read the value from first returned result.
Based on your code try this
<?php
mysql_connect("localhost", "root", "*********");
mysql_select_db("3591_other");
$result = mysql_query("SELECT * FROM rivase_f_users WHERE user_name = '$email' AND user_pass=sha1('$passwd')");
if (!$result) {
echo mysql_error();
}
//snip
else if ($row=mysql_fetch_assoc($result)) {
$_SESSION['username']=$email;
if ($row['user_level']==1) {$_SESSION['usertype']='leader';}
header("Location: index.php");
die();
}
?>
I've changed the else if check to save the row when doing the comparison.

Categories