How to include a PHP variable inside a MySQL statement

0 votes

When I put the variable $type inside VALUES then this doesn't work. What am I doing wrong?

$type = 'testing';
mysql_query("INSERT INTO contents (type, reporter, description) 
     VALUES($type, 'john', 'whatever')");

Thanx in advance!

Apr 9, 2020 in PHP by kartik
• 37,480 points
525 views

1 answer to this question.

0 votes

Hii,

You can try this:

$query="SELECT * FROM CountryInfo WHERE Name = '".$name."'";

and you define $name whatever you want.
And another way, the complex way, is like that:

$query = " SELECT '" . $GLOBALS['Name'] . "' .* " .
         " FROM CountryInfo " .
         " INNER JOIN District " .
         " ON District.CountryInfoId = CountryInfo.CountryInfoId " .
         " INNER JOIN City " .
         " ON City.DistrictId = District.DistrictId " .
         " INNER JOIN '" . $GLOBALS['Name'] . "' " .
         " ON '" . $GLOBALS['Name'] . "'.CityId = City.CityId " .
         " WHERE CountryInfo.Name = '" . $GLOBALS['CountryName'] .
         "'";
answered Apr 9, 2020 by Niroj
• 82,540 points

Related Questions In PHP

0 votes
1 answer

How to remove a variable from a PHP session array?

Hello @kartik, Try: if (isset($_POST['remove'])) { ...READ MORE

answered Nov 8, 2020 in PHP by Niroj
• 82,540 points
148 views
0 votes
1 answer

How to access a global variable in a PHP function?

Hello @kartik, It is not working because you have to ...READ MORE

answered Nov 10, 2020 in PHP by Niroj
• 82,540 points
88 views
0 votes
1 answer

How to discard a variable from a PHP session array

Hello, Try this: if (isset($_POST['remove'])) { ...READ MORE

answered Nov 13, 2020 in PHP by Niroj
• 82,540 points
36 views
0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,540 points
681 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,540 points
4,585 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,540 points
516 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,540 points
392 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,540 points
5,215 views
0 votes
1 answer

How to prevent direct access to a php include file?

Hello @kartik, Add this to the page that ...READ MORE

answered Oct 1, 2020 in PHP by Niroj
• 82,540 points
73 views
0 votes
1 answer

How to prevent direct access to a php include file?

Hello @kartik, Add this to the page that ...READ MORE

answered Oct 18, 2020 in PHP by Niroj
• 82,540 points
313 views