PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 14, 2022

[FIXED] Why am I getting a syntax error while running SQL query in PHP?

 December 14, 2022     php, sql, syntax, syntax-error     No comments   

Issue

I am new to PHP and SQL and trying run a code, which has run into a problem. Connected the SQL, included the PHP files and was working fine.

However, the moment I added the SQL query, I am getting an error

syntax error, unexpected token". Unexpected 'Name'. Expected ';'.intelephense(1001)

How can I fix this?

<?php
include 'partials/_header.php';
include 'partials/_dbconnect.php';
SELECT * FROM `categories` WHERE category_id=1;
?>

Solution

This is the correct way to run a query in PHP

partials/_dbconnect.php:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

the code

<?php
include 'partials/_header.php';
include 'partials/_dbconnect.php';

$result = $mysqli->query("SELECT Name FROM City LIMIT 10");
printf("Select returned %d rows.\n", $result->num_rows);


Answered By - Digen Prime Apps
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing