• PHP Help
  • PHP Fatal error: Uncaught Error: Call to a member function prepare() on null in

Hello, I really need help, so when I was running this in my local server it was working fine, but once I uploaded to Hostgator is giving me this error:
[21-Jul-2020 16:53:55 America/Chicago] PHP Fatal error: Uncaught Error: Call to a member function prepare() on null in /home4/l3mxta7i/plantsapp.tuiglesia.online/php/data.php:18
Stack trace:
#0 /home4/l3mxta7i/plantsapp.tuiglesia.online/proceso/add_new.php(53): loadFamilias()
#1 {main}
thrown in /home4/l3mxta7i/plantsapp.tuiglesia.online/php/data.php on line 18

The Code in data.php is =
`<?php
require 'fills_connect.php';

if(isset($_POST['aid'])) {
	$db = new DbConnect;
	$conn = $db->connect();

	$stmt = $conn->prepare("SELECT * FROM ajaxplant_type WHERE family_id = " . $_POST['aid']);
	$stmt->execute();
	$plantas = $stmt->fetchAll(PDO::FETCH_ASSOC);
	echo json_encode($plantas);
}

function loadFamilias() {
	$db = new DbConnect;
	$conn = $db->connect();
	$stmt = $conn->prepare("SELECT * FROM ajaxdata_family");
	$stmt->execute();
	$familias = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $familias;
}



if(isset($_POST['diay'])) {
	$db = new DbConnect;
	$conn = $db->connect();

	$stmt = $conn->prepare("SELECT * FROM ajax_tipo WHERE id_process = " . $_POST['diay']);
	$stmt->execute();
	$procesos = $stmt->fetchAll(PDO::FETCH_ASSOC);
	echo json_encode($procesos);
}

function loadProcedimiento() {
	$db = new DbConnect;
	$conn = $db->connect();

	$stmt = $conn->prepare("SELECT * FROM ajax_process");
	$stmt->execute();
	$acciones = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $acciones;
}

?>

The Code in line 53 in add_new.php is= <?php
require '../php/data.php';
$familias = loadFamilias();
var_dump($familias);exit;
foreach ($familias as $family) {
echo "<option id='".$family['id']."' value='".$family['id']."'>".$family['family']."</option>";

                        }
                       ?>`

How Can I fix this?

Presumably these are failing...

$conn = $db->connect();

...so you may need to check the php error logs or add some debug code to find out why, e.g.:

$conn = $db->connect();
if(!$conn) {
  // use appropriate function for whatever DB extension you are using
  // to output what the DB connection error was
}

    pablomoracantero $stmt = $conn->prepare("SELECT * FROM ajax_process");

    This isn't part of the question you've got (sorry) but you don't need to prepare this statement. The whole point of using prepared statements is to avoid SQL injection - if you're not using a variable in the query, there's no chance of injecting anything.

    pablomoracantero $stmt = $conn->prepare("SELECT * FROM ajax_tipo WHERE id_process = " . $_POST['diay']);

    And this is being prepared incorrectly. Use placeholders, then bind the variables to the placeholders - you're using PDO so you can either use bindParam() or just pass the bindings to execute().

      Write a Reply...