Fatal error with fetch_array(mysqli_both) - php

I am receiving a "Fatal error" once the function fetch_array(mysqli_both) is invoked. The file below on the while instance is exactly where the error is coming from.
<?php
include 'connect.php';
$id = $_SESSION['user_id'];
$res = mysqli_query($mysqli, "SELECT a.name,j.link,j.points, a.submit_time from journal j, article a where a.journal_id = j.id and a.professor_id = $id");
?>
<table>
<tr>
<th>Article</th>
<th>Journal</th>
<th>Points</th>
<th>Time</th>
</tr>
<?php
while ($row = $res->fetch_array(MYSQLI_BOTH)) {
?>
<tr>
<td><?php echo $row[0] ?></td>
<td><?php echo $row[1] ?></td>
<td><?php echo $row[2] ?></td>
<td><?php $date = date_create($row[3]);
echo date_format($date, 'Y-m-d H:i:s'); ?></td>
<tr>
<?php
}
?>
</table>
When i try it on my wampserver i don't have this problem as i activated couple of PHP extensions. Online, i don't think it's easy to activate such extensions. Do you think this error is related to the fact that i didn't invoked the $query as a variable with $mysqli in line 4?

First of all you should use require_once('connect.php'); because it is a needed file for this code.
Also you have to check your $mysqli variable if it's a correct connection to your database.
For more help you might have to post the full Error-Message

you should check result before fetch:
if ($res) {
while ($row = $res->fetch_array(MYSQLI_BOTH)) {
...
...
}
} else {
echo printf("Errormessage: %s\n", mysqli_error($mysqli));
}

Related

Trying to get my sql table to have pagination but the results aren't showing up

I've been turning in circles for a while now trying to understand what I have done wrong with lines 68 and 110. It comes up with:
"expects parameter 1 to be resource, boolean given"
but I don't understand where my mistake is. Would someone be able to point me in the right direction or explain my error so I can better understand?
I'm really new to PHP (only started learning it last week) so I've been mostly doing my best with tutorials and what I can find online.
Line 68 onwards:
while($row = mysql_fetch_array($query)){
$card_number = $row['card_number'];
$card_id = $row['card_id'];
$card_name = $row['card_name'];
$card_mana_img = $row['card_mana_img'];
$card_type = $row['card_type'];
$card_rarity = $row['card_rarity'];
$card_set = $row['card_set'];
}
?>
Lines 99 onwards:
<table>
<tr>
<td>Number</td>
<td>Name</td>
<td>Type</td>
<td>Mana</td>
<td>Rarity</td>
<td>Set</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $card_number; ?></td>
<td><?php echo $card_name; ?></td>
<td><?php echo $card_type; ?></td>
<td><?php echo $card_mana_img; ?></td>
<td><?php echo $card_rarity; ?></td>
<td><?php echo $card_set; ?></td>
</tr>
<?php } ?>
</table>
<br>
<?php echo $paginationCtrls; ?><br>
<?php echo $textline2;?><br>
<?php echo $textline1;?>
Don't use this: $sql = "SELECT number FROM magicorigins_cardset";
Use this:
$sql = "SELECT count(*) FROM magicorigins_cardset";

Can't list result from sql to html table

Trying to list the data from mysql to a html table using php in main html file. I've been through all of the other questions on here and I'm sure I have mostly the same methods and code as them.
For some reason (which I suspect has something to do with mysql and not the code) the only result is a blank table with one row and five columns. Each time I try to implement the other codes they just seem to print text onto the site.
I'm very confused as I think I've done the right thing. I've even been able to list the data from mysql through php echo so I know it's there and that I can access it. Really would appreciate some help on this. Thank you.
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tbody>
</table>
You say this code is in your mail html file? And it is printing out onto the screen? Try changing your file to .php not .html! Php code won't run in a .html file, and will likely output your code directly onto the page.
Mysqli_query expect connection link as first parameter.
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
Just a quick not so related improvement. You can avoid inserting most of the php opening and closing clauses:
...
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['title']."/td>";
echo "<td>".$row['url']"</td>";
...
}
?>
Use <tr> because table must have atleast one row(<tr>) and one column(<td>)
<table border="1">
<tbody>
<tr>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tr>
</tbody>
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
if (!$results) {
mysqli_error($results);
die();
}
while ($row = mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['url'] ?></td>
<td><?php echo $row['details'] ?></td>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['submission_date'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>

How to search and output with a form input value using PDO

I am trying to perform a search action by getting a value from HTML form using PDO and display the results in the same page. I am getting no errors also no results. Please advise! i am beginner.
<?php
require_once 'db_alternate2.php';
session_start();
if (isset($_POST['submit'])) {
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$payrl_search = trim($_POST['empname_search']);
$sql = "SELECT * FROM payroll_db
WHERE 'pay_staff_name'
LIKE '$%payrl_search%'";
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
}
my php code inside html is
<table>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['pay_emp_id'])?></td>
<td><?php echo htmlspecialchars($r['pay_staff_name']); ?></td>
<td><?php echo htmlspecialchars($r['pay_month_sal']); ?></td>
<td><?php echo htmlspecialchars($r['pay_amount']); ?></td>
<td><?php echo htmlspecialchars($r['pay_bankname']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
I am getting these errors:
Undefined variable: q
Fatal error: Call to a member function fetch() on a non-object
i think my error part is
<?php while ($r = $q->fetch()): ?>

Add Echo in PHP

underneath is a small script which shows if someone has a birthday today.
The only thing what i miss is a line under the name with the text "Happy birthday" It should only appear, ofcourse is someone has a birthday. how can i achieve this?
<html>
<head>
<title>Vandaag Jarig:</title>
</head>
<body>
<table>
<thead>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "****");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("my_site_db");
$results = mysql_query
("SELECT * FROM aevinew2_verjaardagen WHERE DAY(geboortedatum) = DAY (CURDATE ()) AND MONTH(geboortedatum) = MONTH(CURDATE())");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Naam']?></td>
<td><?php echo $row['Afdeling']?></td>
</tr>
<?php
}
}else{
echo "Helaas geen taart vandaag, er is niemand jarig";
}
?>
</tbody>
</table>
</body>
</html>
Something like this? You had syntax error in your code aswell. Missing ; delimiter at end of echo command.
<td><?php echo $row['Naam'].'<br/>Happy Birthday!'; ?></td>
<td><?php echo $row['Afdeling']; ?></td>
Replace line 22 ,23 with these lines. You are missing ';' after echo statement.
<td><?php echo $row['Naam'].'<br/>Happy Birthday!'; ?></td>
<td><?php echo $row['Afdeling']; ?></td>
well, the script is working (it was already), but the only thing i need is an echo under the current echo which is just showing some text.
If i do that it letterly puts that kind of text on top include the text Echo

What's wrong with this mysqli query?

I'm trying to figure out what's wrong with my mysqli code below. The data is definitely in the table. I have 2 questions:
Why isn't this giving me any results?
Could I have the echo <table></table> section in a separate php file, if yes, how would I call $name,$partner,$cell etc.?
<?php
$mysqli = new mysqli($host, $uname, $pword, $db);
if ($mysqli->connect_errno)
{
echo "Connection failed: %s\n", $mysqli->connect_error;
exit();
}
$query = ("SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC");
$result = $mysqli->query($query);
while ($row = $result->fetch_array()) {
$space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : '';
$name = $row['firstname'].$space.$row['lastname'];
$partner = $row['spousefirst'];
$cell = (!empty($row['phonecell'])) ? " {$row['phonecell']} (cell)" : '';
$email = (!empty($row['email'])) ? " {$row['email']} (email)" : '';
$ID = $row['ID'];
echo'<table>
<tbody>
<tr>
<td><input type="checkbox" name="" id="" value="<? echo $ID; ?>"></td>
<td><strong><? echo $name; ?></strong></td>
<td><? echo $partner; ?></td>
<td><? echo $phonecell; ?></td>
<td><? echo $email; ?></td>
<td><? echo date("M jS, g:i A", strtotime($date)); ?></td>
<td><? echo $contacttype; ?></td>
<td><? echo $agentassigned; ?></td>
<td><? echo $leadstatus; ?></td>
<td>View + </td>
<td>View + </td>
<td>D</td>
</tr>
</tbody>
</table>';
}
?>
EDIT:
It's showing the <table> but just not giving me values for $name, $email...ect.
The final echo does not look well constructed. I would prefer to use a concatenated string to output the variables, just like this( I put only a few lines as example):
echo'<table>
<tbody>
<tr>
<td><input type="checkbox" name="" id="" value="'. $ID .'"></td>
<td><strong>'. $name.' </strong></td>
The query looks solid as does the rest of your code. Here are a few troubleshooting steps to try out:
echo out $query and run it directly against your database. Do you get results? If you don't get any results from a direct query then that means the query is returning 0 rows and your tables will end up empty as well.
Do a var_dump($result) to verify there is information being stored in there. You could also add change $result = $mysqli->query($query); to $result = $mysqli->query($query) or die $mysqli->error;
The results from these 2 steps should give you a good idea on where things are going wrong. In general you want to stop through each part of the query process and look at every variable involved to ensure that it has the data you expect stored inside of it.
To answer your 2nd question: yes you can. If you put the code for the table in a separate file and then include()it, the variables will still be available to the included code.
See the PHP manual on include()

Categories