PhpEd Debugger show variable empty value - php

Does anyone know why debugger shows an empty value for $result and $mysqli in that script?
<?php
// show SELECT result as HTML table
function show_table($result) {
if(!$result) {
echo "<p>No valid query result.</p>\n";
return;
}
if($result->num_rows>0 && $result->field_count>0) {
echo "<table>";
// column headings
echo "<tr>";
foreach($result->fetch_fields() as $meta)
printf("<th>%s</th>", htmlspecialchars($meta->name));
echo "</tr>\n";
// content
// row fetch row
while($row = $result->fetch_row()) {
echo "<tr>";
foreach($row as $col)
printf("<td>%s</td>", htmlspecialchars($col));
echo "</tr>\n";
}
echo "</table>\n";
}
}
require_once 'password.php';
// connect to MySQL
$mysqli = new mysqli($mysqlhost, $mysqluser, $mysqlpasswd, $mysqldb);
if(mysqli_connect_errno()) {
echo "<p>Sorry, no connection! ", mysqli_connect_error(), "</p>\n";
exit();
}
// show SELECT result with show_table
if($result = $mysqli->query("SELECT * FROM titles")) {
show_table($result);
$result->close();
}
// disconnect
$mysqli->close();
?>
</body></html>
<?php
?>

hey DOD i think you $mysqldb is null because when its null you never revise any error or warning
i test it in other case such as without user name or password.
$mysqli = new mysqli($mysqlhost, $mysqluser, $mysqlpasswd, $mysqldb);

Related

Fields only echoed when value

To avoid open rows on the screen, how can I reach that the 5 vip fields, only are echoed when there is a value in it?
Thanks, Benny
$db = new mysqli('host', 'user', 'pass', 'dbase');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "
SELECT
user.FName,
user.LName,
user.HerbalifeID,
user.UplineS,
registratie.PartnerFName as pfn,
registratie.PartnerLName as pln,
registratie.NaamVIP1 as vip1,
registratie.NaamVIP2 as vip2,
rgistratie.NaamVIP3 as vip3,
registratie.NaamVIP4 as vip4,
registratie.NaamVIP5 as vip5
FROM
registratie
INNER JOIN user
ON registratie.userID = user.UserID
AND registratie.eventID =127;
";
$result = $db->query($query) or die($db->error.__LINE__);
if ($result->num_rows) {
while ($row = $result->fetch_object()) {
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>
{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>"; //only
}
} else {
echo 'No Results';
}
if(!empty({$row->vip1}) and !empty({$row->vip2}) and !empty({$row->vip3}) and !empty({$row->vip4}) and !empty({$row->vip5}) ){
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";
}else{
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln})<br><br>";
}
I don't know if I get it.
If you want to print only when all vips are filled, use the following code:
//...
$range = range(1,5);
while ($row = $result->fetch_object()) {
//This will be printed in every iteration
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}";
//if one of the vips has no value, go to the next $row
foreach($range as $r){
$vipColumn = "vip$r";
if(empty($row->$vipColumn)){
continue 2; //breaks both 'foreach' and 'while' loops
}
}
//This only echo when all vips has values
echo "<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";
}
//...

PHP : to display only new row of a table in bold and rest without any formating (data will be entered by the user)

I need to display every new row in bold that will be stored in database in php and and then display it. Only the new row should be bold. Here's what I did
<?php
//connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "resultdb"; //my database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<?php
$name=$_POST['name'];
$roll=$_POST['roll'];
$m1=$_POST['m1'];
$m2=$_POST['m2'];
$m3=$_POST['m3'];
//ISNERT DATA INTO TADABASE
$sql="INSERT INTO markstb(name,rollno,sub1,sub2,sub3) VALUES('$name','$roll','$m1','$m2','$m3')";
if($conn->query($sql)===true)
{
//ketp id column in DB to auto_increment
$last_id=mysqli_insert_id($conn);
echo " NEW RECORD INSERTED"."<br>";
if($last_id>0)
{
$sql3="SELECT * FROM markstb WHERE id='$last_id'";
$result = $conn->query($sql3);
echo "<table border='2' bordercolor='black' ";
echo"<tr><td>NAME</td><td>ROLLNO</td><td>SUB1</td><td>SUB2</td><td>SUB3</td></tr>";
while($row=$result->fetch_assoc())
{
echo"<tr><td><b>".$row['name']."</td><td><b>".$row['rollno']."</td><td><b>".$row['sub1']."</td>"
."<td><b>".$row['sub2']."</td><td><b>".$row['sub3']."</td>";
}
echo "</table>";
}
else
{
echo"check last_id";
}
echo "<br><br><br><br>";
}
else
{
echo "ERROR".$sql."<br>".$conn->error;
}
//RETRIVE DATA FROM DATABASE
$sql2="SELECT * FROM markstb ORDER BY name";
//$result=$conn->query($sq2);
$result = $conn->query($sql2);
echo "<table border='2' bordercolor='black' ";
if($result->num_rows > 0)
{
echo"<tr><td>NAME</td><td>ROLLNO</td><td>SUB1</td><td>SUB2</td><td>SUB3</td></tr>";
while($row=$result->fetch_assoc())
{
echo"<tr><td>".$row['name']."</td><td>".$row['rollno']."</td><td>".$row['sub1']."</td>"
."<td>".$row['sub2']."</td><td>".$row['sub3']."</td>";
}
echo "</table>";
}
else
{
echo" 0 rows";
}
$conn->close();
?>
In above program I have ony fetched the row which is new and made it bold
but I have to show all data that is in database and make only row bold which is new, which is entered by the user.
In your last while loop you could try:
if($row['id'] == $last_id){
//echo "your row that is
//bold
}else{
// echo you regular row
}
When you are printing out the data, you can do this type of thing inside of the echo statement:
echo "<tr" . $last_id === (int) $row['id] ? . ' class="bold"' : null; . "><td>...";
So that means, when you are printing the tag, you are checking if the row id matches the last id, if so you are additionally printing a html class of "bold".
Of course, it's up to you to use css to target this selector and actually make it bold.
That is called the 'ternary if'. It looks like this:
condition ? do this if true : do this if false;

Connect to mysql db but no data from table is echo to screen

The script below connects to the db (I get the connected successfully echo) but none of the data from the query is shown onscreen.
I assume the data must be somewhere as I do not get the error message.
Question: Where is the error in the script?
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
while($row = mysql_fetch_array($result))
if ($result === "") {echo "An error occurred.";}
{
echo $row['graduation_year'];
echo "<br>";
}
?>
Appreciate any help that can be sent my way, I'm a real newbie at this stuff.
Roger
Try adding an opening brace after while($row = mysql_fetch_array($result)) and a closing brace before the end of the script.
Is this not a syntax issue?? Why is there an IF clause after a WHILE clause but before the opening bracket for the WHILE loop block?
Additionally, you are trying to use mysql_fetch_array() instead of mysqli_fetch_array().
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
if ($result !== FALSE && mysqli_num_rows($result) > 0) { // Proper way to test for results
while($row = mysqli_fetch_assoc($result))
{
echo $row['graduation_year'];
echo "<br/>";
}
}
else {
die("Query Returned 0 rows...");
}
?>
Documentation: mysqli_result::$num_rows

Moving data from one mysql table to another

When moving data from on table to another I get Error in query: Duplicate entry '0' for key 'PRIMARY'
I dont care to copy the primary key I would like each table to have its own primary key- this table will just hold data to be processed,checked and released by person them moved to a final table that will contain all processed data.
<basefont face="Arial">
<title>QA-1160 Search</title>
</head>
<body>
<?php
// include the page Header
include('header.php');
?>
<?php
//retrieve session data
echo $_SESSION['mnumber'];
echo "<P>";
$mnumber=$_SESSION['mnumber'];
$amnumber=$mnumber;
$mnumber=" '".$mnumber."' ";
// set database server access variables:
$host = "localhost";
$user = "test";
$pass = "test";
$db = "test";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database :)!");
// create query
$query = "insert into testingqa1160 (material, test, sample, frequency, stp, rtr, notes, usl, lsl) SELECT material, test, sample, frequency, stp, rtr, notes, usl, lsl FROM qa1160 WHERE material=";
$query=$query.$mnumber;
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// mysql_free_result($result);
// close connection
mysql_close($connection);
// clear session
session_unset();
session_destroy();
// load test data
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database :)!");
// create query
$query = "SELECT * FROM testingqa1160";
// $query=$query.$mnumber;
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<center><table cellpadding=5 border=1>";
echo "<tr>";
echo "<center>";
echo "<td>"."ID"."</td>";
echo "<td>"."Material"."</td>";
echo "<td>"."Test"."</td>";
echo "<td>"."Sample"."</td>";
echo "<td>"."Frequency"."</td>";
echo "<td>"."STP"."</td>";
echo "<td>"."Release"."</td>";
echo "<td>"."Notes"."</td>";
echo "<td>"."LSL"."</td>";
echo "<td>"."USL"."</td>";
echo "</center></tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo "<td>".$row[6]."</td>";
echo "<td>".$row[7]."</td>";
echo "<td>".$row[9]."</td>";
echo "<td>".$row[8]."</td>";
echo "</tr>";
}
echo "</table></center>";
echo "</center>";
}
else {
// no
// print status message
echo "<center><FONT SIZE=18>";
echo $_GET["mnumber"];
echo " Materail is not found! </font>";
echo "</center>";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
<td>Testing</td>
<?php
// include the page footer
include('footer.php');
?>
</body>
</html>
1.- Don't use mysql_* functions, they are deprecated, use mysqli or PDO
2.- Your table testingqa1160 need to have the auto-increment attribute to the id column

mysqli->execute get error but no error is output

I have the next php code:
<?php
mysqli_report(MYSQLI_REPORT_ALL);
$mysqli = new mysqli("localhost","mybd","mypass");
if ($mysqli->connect_errno) { echo "Error connect<br/>"; }
else {
$mysqli->select_db("database1");
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]); // shows correct database selected
$result->close();
}
$sentencia = $mysqli->prepare("select pass from users Where name ='ronald'");
echo "Prepare error:".$mysqli->error."<br/>";
if (!$sentencia) echo "<br/>sentencia is null<br/>";
if ($sentencia->execute)
{
$sentencia->bind_result($cpass);
$sentencia->fetch();
echo "Passwd:".$cpass."<br/>";
$con="checkpass";
if (($con!=$cpass) && (md5($con)!=$cpass))
{
echo "OK<br/>";
}
else echo "NO OK<br/>";
}
else echo "<br/>Error execute: ".$mysqli->error;
}
mysqli_report(MYSQLI_REPORT_OFF);
?>
Problems are:
- $mysqli->error shows nothing. No error. Always empty string.
- $sentencia->execute always return null, and then always echo "Error execute:", but no information about error.
Database selected shows ok. It select the right database. This is an example. Really the name is passed with "$sentencia->bind_param("s",$user);" but with this, I get apache error of "no object".
I don't know why it happens. The SQL is checked and is Ok.
Thanks.
Shouldn't execute be a function nor property?
http://php.net/manual/en/mysqli-stmt.execute.php
if ($sentencia->execute())
{
}

Categories